package src.core;

import src.entity.Bullet;
import src.entity.Player;
import src.entity.Zombie;
import src.render.bodyDraw;
import src.render.playerDraw;
import src.render.zombieDraw;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class GamePanel extends JPanel {

    private static final int PANEL_WIDTH = 1280;
    private static final int PANEL_HEIGHT = 500;

    java.util.List<Player> players = new ArrayList<>();
    java.util.List<Zombie> zombies = new ArrayList<>();
    // 存放玩家和脏比的List容器

    java.util.List<Bullet> bullets = new ArrayList<>();
    // 场景中的子弹

    java.util.List<Zombie> bodys = new ArrayList<>();
    // 被打死的脏比

    int time = 0;  // 游戏刻计时器

    public GamePanel() {
        setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));  //手工设置画幅大小，修复白屏bug
        setBackground(Color.GRAY);
    }

    public void paintBackground(Graphics g) {
        Image originalImage = new ImageIcon("src/resource/img/地板.png").getImage();
        g.drawImage(originalImage, 0, 0, PANEL_WIDTH, PANEL_HEIGHT, null);
    }

    public void paintBackgroundItem(Graphics g) {
        Image originalImage = new ImageIcon("src/resource/img/上层图层.png").getImage();
        g.drawImage(originalImage, 0, 0, PANEL_WIDTH, PANEL_HEIGHT, null);
    }

    public void paintPlayer(Graphics g) {
        playerDraw.draw(g, players);  // 因为要做图片缩放，代码比较多，所以直接写了个静态工具类
    }

    public void paintZombie(Graphics g) {
        zombieDraw.draw(g, zombies);
    }

    public void paintBullet(Graphics g) {
        g.setColor(Color.YELLOW);
        Font font = new Font("微软雅黑", Font.BOLD, 20);
        g.setFont(font);
        for (Bullet bullet : bullets) {
            g.drawString("-", bullet.getX(), bullet.getY());  // 子弹姑且设计成长这样吧，我没找到贴图
        }
    }

    public void paintText(Graphics g) {
        int sum = 0;
        for (Player player : players) {
            sum += player.getScore();
        }
        g.setColor(Color.YELLOW);
        Font font = new Font("微软雅黑", Font.BOLD, 35);
        g.setFont(font);
        g.drawString("当前分数：" + sum, PANEL_WIDTH - 300, PANEL_HEIGHT - 55);
    }

    public void paintBody(Graphics g) {
        bodyDraw.draw(g, bodys);
    }


    public void paint(Graphics g) {
        super.paint(g);
        this.paintComponent(g); // 清除背景
        this.paintBackground(g); // 先绘制地板

        this.paintBody(g);

        this.paintText(g);
        this.paintPlayer(g);
        this.paintZombie(g);
        this.paintBullet(g);


        this.paintBackgroundItem(g);  // 立体景物总在最上层
    }

    public void action() {
        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (time % 1000 == 0) {
                    Zombie.spawnZombie(zombies);      //每隔一定间隔生成脏比，生成频率即上一行取余数
                }
                if (time % 100 == 0) {
                    for (int i = 0; i < zombies.size(); i++) {
                        zombies.get(i).hitbox(bullets, players);
                        zombies.get(i).move();
                        if (zombies.get(i).isDead()) {
                            bodys.add(zombies.get(i));
                        }
                        if (zombies.get(i).getX() < 0 || zombies.get(i).isDead()) {
                            zombies.remove(i);          //超出边框直接移除
                        }
                    }

                    for (Player player : players) {
                        bullets.add(player.shoot(player.getX(), player.getY(), player.getId())); // 每个玩家射击
                    }

                }
                for (int i = 0; i < bullets.size(); i++) {
                    bullets.get(i).move();
                    if (bullets.get(i).getX() > 3000 || !bullets.get(i).isAlive()) {
                        bullets.remove(i);
                    }
                }

                repaint();
                time += 10;
            }
        }, 0, 10);
    }

    public void setPlayerId(int id) {
        for (int i = 1; i <= id; i++) {
            players.add(new Player(i));
        }
        //我们自己的玩家对象初始化
        //System.out.println(players.get(0).x+" "+players.get(0).y);
    }

    public void updatePlayerPositions(String serverMessage) {
        try {
            //serverMessage = serverMessage.substring(1);
            String[] positions = serverMessage.split(";");
            // 分隔得到每个用户的id和坐标

            for (int i = 0; i < positions.length; i++) {
                String[] coords = positions[i].split(",");

                if (positions.length > players.size()) {
                    setPlayerId(Integer.parseInt(coords[0]));
                }
                // 如果和服务器发来的玩家信息对不上，说明我们前面有其他玩家登录了，为他们注册对象

                //System.out.println(coords[0]+" "+coords[1]+" "+coords[2]);
                players.get(i).setX(Integer.parseInt(coords[1]));
                players.get(i).setY(Integer.parseInt(coords[2]));
            }
        } catch (Exception e) {
            System.err.println("解析位置信息出错: " + e.getMessage());
        }
    }
}
